package com.zhangtai.Neusoft.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.Neusoft.constVo.EmployeeChangeConst;
import com.zhangtai.Neusoft.vo.CredentialVo;
import com.zhangtai.Neusoft.vo.FundsVo;
import com.zhangtai.Neusoft.vo.SecurityVo;
import com.zhangtai.modules.dao.CredentialInfoDao;
import com.zhangtai.modules.entity.credential.CredentialHangEntity;
import com.zhangtai.modules.entity.credential.CredentialInfoEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.security.FundsRecordEntity;
import com.zhangtai.modules.entity.security.SecurityEntity;
import com.zhangtai.modules.entity.security.SecurityRecordEntity;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.security.FundsRecordService;
import com.zhangtai.modules.service.security.SecurityRecordService;
import com.zhangtai.mq.MessageSender;
import com.zhangtai.mq.config.EmployeeChangeMqConfig;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class NeusoftSecurityService {
    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Autowired
    private MessageSender messageSender;

    @Autowired
    private EmployeeChangeMqConfig employeeChangeMqConfig;

    @Autowired
    private CredentialInfoDao credentialInfoDao;

    @Async("Neusoft")
    public R uploadSecurity(List<SecurityRecordEntity> recordList,Date date){
        if(CollectionUtils.isEmpty(recordList)){
            return R.error();
        }
        SecurityVo security = new SecurityVo();
        BigDecimal injuryInsuranceOrg = new BigDecimal(0);
        BigDecimal unemploymentInsuranceOrg = new BigDecimal(0);
        BigDecimal unemploymentInsurancePerson = new BigDecimal(0);
        BigDecimal maternityInsuranceOrg = new BigDecimal(0);
        BigDecimal orgCostTotal = new BigDecimal(0);
        BigDecimal endowmentInsuranceOrg = new BigDecimal(0);
        BigDecimal personCostTotal = new BigDecimal(0);
        BigDecimal medicalInsuranceOrg = new BigDecimal(0);
        BigDecimal medicalInsurancePerson = new BigDecimal(0);
        BigDecimal endowmentInsurancePerson = new BigDecimal(0);
        BigDecimal medicalInsuranceBase = new BigDecimal(0);
        BigDecimal endowmentInsuranceBase = new BigDecimal(0);
        BigDecimal unemploymentInsuranceBase = new BigDecimal(0);
        BigDecimal injuryInsuranceBase = new BigDecimal(0);
        BigDecimal maternityInsuranceBase = new BigDecimal(0);
        int i=0;
        for(SecurityRecordEntity recordEntity:recordList){
//            SecurityRecordEntity oldRecord =  securityRecordService.getByIdE(recordEntity.getRecordId());
            if(i==0){
                EmployeeInfoEntity infoEntity = employeeInfoService.getByIdE(recordEntity.getOid());
                security.setDanwei(recordEntity.getUnitName());
                security.setApplyCode(recordEntity.getOperateCode());
                security.setEmpCode(infoEntity.getCode());
                security.setEmpName(infoEntity.getName());
                security.setId(recordEntity.getRecordId());
                security.setJdate(DateUtils.format(date));

            }
            if(!ObjectUtils.isEmpty(recordEntity.getEndowmentInsuranceBase())){
                security.setSbBase(recordEntity.getEndowmentInsuranceBase());
            }
            injuryInsuranceOrg  = getBigDecimal(injuryInsuranceOrg,recordEntity.getInjuryInsuranceOrg());
            unemploymentInsuranceOrg  = getBigDecimal(unemploymentInsuranceOrg,recordEntity.getUnemploymentInsuranceOrg());
            unemploymentInsurancePerson = getBigDecimal(unemploymentInsurancePerson,recordEntity.getUnemploymentInsurancePerson());
            maternityInsuranceOrg  = getBigDecimal(maternityInsuranceOrg,recordEntity.getMaternityInsuranceOrg());
            orgCostTotal  = getBigDecimal(orgCostTotal,recordEntity.getOrgCostTotal());
            endowmentInsuranceOrg = getBigDecimal(endowmentInsuranceOrg,recordEntity.getEndowmentInsuranceOrg());
            personCostTotal  = getBigDecimal(personCostTotal,recordEntity.getPersonCostTotal());
            medicalInsuranceOrg = getBigDecimal(medicalInsuranceOrg,recordEntity.getMedicalInsuranceOrg());
            medicalInsuranceOrg = getBigDecimal(medicalInsuranceOrg,recordEntity.getLargerMedicalOrg());
            medicalInsuranceOrg = getBigDecimal(medicalInsuranceOrg,recordEntity.getDisabledEmploymentOrg());
            medicalInsurancePerson  = getBigDecimal(medicalInsurancePerson,recordEntity.getMedicalInsurancePerson());
            medicalInsurancePerson  = getBigDecimal(medicalInsurancePerson,recordEntity.getLargerMedicalPerson());
            medicalInsurancePerson  = getBigDecimal(medicalInsurancePerson,recordEntity.getDisabledEmploymentPerson());
            endowmentInsurancePerson  = getBigDecimal(endowmentInsurancePerson,recordEntity.getEndowmentInsurancePerson());
            medicalInsuranceBase  = getBigDecimal(medicalInsuranceBase,recordEntity.getMedicalInsuranceBase());
            endowmentInsuranceBase  = getBigDecimal(endowmentInsuranceBase,recordEntity.getEndowmentInsuranceBase());
            unemploymentInsuranceBase  = getBigDecimal(unemploymentInsuranceBase,recordEntity.getUnemploymentInsuranceBase());
            injuryInsuranceBase  = getBigDecimal(injuryInsuranceBase,recordEntity.getInjuryInsuranceBase());
            maternityInsuranceBase  = getBigDecimal(maternityInsuranceBase,recordEntity.getMaternityInsuranceBase());
           i++;
        }
        security.setGsCorp(injuryInsuranceOrg);
        security.setSyCorp(unemploymentInsuranceOrg);
        security.setSyPsn(unemploymentInsurancePerson);
        security.setSyuCorp(maternityInsuranceOrg);
        security.setTotalCorp(orgCostTotal);
        security.setYlCorp(endowmentInsuranceOrg);
        security.setTotalPsn(personCostTotal);
        security.setYliaoCorp(medicalInsuranceOrg);
        security.setYliaoPsn(medicalInsurancePerson);
        security.setYlPsn(endowmentInsurancePerson);
        security.setYliaoBase(medicalInsuranceBase);
        security.setYlBase(endowmentInsuranceBase);
        security.setSyBase(unemploymentInsuranceBase);
        security.setGsBase(injuryInsuranceBase);
        security.setSyuBase(maternityInsuranceBase);
        List<SecurityVo> list = new ArrayList<>();
        list.add(security);
        JSONObject object = new JSONObject();
        object.put("sbInputVOS",list);
        sendMq(object, EmployeeChangeConst.SECURITY_TYPE_SECURITY);
        return R.ok();
    }

    @Async("Neusoft")
    public R uploadFunds(List<FundsRecordEntity> recordList,Date date){
        FundsVo vo = new FundsVo();
        if(CollectionUtils.isEmpty(recordList)){
            return R.error();
        }
        int i=0;
        BigDecimal reservedFundsOrg = new BigDecimal(0);
        BigDecimal reservedFundsPerson = new BigDecimal(0);
        for(FundsRecordEntity recordEntity:recordList){
//            FundsRecordEntity oldRecord = fundsRecordService.getByIdE(recordEntity.getRecordId());
            if(i==0){
                EmployeeInfoEntity infoEntity = employeeInfoService.getByIdE(recordEntity.getOid());
                vo.setEmpCode(infoEntity.getCode());
                vo.setIfOk("0");
                vo.setEmpName(infoEntity.getName());
                vo.setIssueMonth(DateUtils.format(date));
                vo.setOid(recordEntity.getRecordId().toString());
                vo.setOperatorCode(recordEntity.getOperateCode());
                if(!ObjectUtils.isEmpty(recordEntity.getReservedFundsBase()))
                vo.setGjjjishu(recordEntity.getReservedFundsBase().toString());
            }
            reservedFundsOrg = getBigDecimal(reservedFundsOrg,recordEntity.getReservedFundsOrg());
            reservedFundsOrg = getBigDecimal(reservedFundsOrg,recordEntity.getOfferFundsOrg());
            reservedFundsPerson = getBigDecimal(reservedFundsPerson,recordEntity.getReservedFundsPerson());
            reservedFundsPerson = getBigDecimal(reservedFundsPerson,recordEntity.getOfferFundsPerson());
            if(!ObjectUtils.isEmpty(recordEntity.getReservedFundsOrgRate()))
                vo.setAcfunCompanyProp(recordEntity.getReservedFundsOrgRate().toString());
            if(!ObjectUtils.isEmpty(recordEntity.getReservedFundsPersonRate()))
                vo.setAcfunPersonProp(recordEntity.getReservedFundsPersonRate().toString());
            i++;
        }
        vo.setAcfunCompany(reservedFundsOrg.toString());
        vo.setAcfunPerson(reservedFundsPerson.toString());
        List<FundsVo> list = new ArrayList<>();
        list.add(vo);
        JSONObject object = new JSONObject();
        object.put("acFunVOScFunVO",list);
        sendMq(object, EmployeeChangeConst.SECURITY_TYPE_FUNDS);
        return R.ok();
    }

    @Async("Neusoft")
    public R uploadCredential(List<CredentialHangEntity> entityList){
        List<CredentialVo> voList = new ArrayList<>();
        Date date = new Date();
        for(CredentialHangEntity entity:entityList){
            CredentialInfoEntity infoEntity = credentialInfoDao.selectOne(new QueryWrapper<CredentialInfoEntity>().eq("is_delete",0).eq("id",entity.getCredentialId()));
            if(!ObjectUtils.isEmpty(infoEntity)){
                EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(infoEntity.getOid());
                CredentialVo vo = new CredentialVo();
                vo.setApplyCode(entity.getApplierCode());
                vo.setBeizhu(entity.getReason());
                vo.setEmpCode(employeeInfoEntity.getCode());
                vo.setEmpName(employeeInfoEntity.getName());
                vo.setFafengyuefen(DateUtils.format(DateUtils.addDateMonths(date,-1),"yyyy-MM")+"-01");
                vo.setHuatGuazheng(entity.getAllowance().toString());
                vo.setId(entity.getId().toString());
                vo.setIfShiyx("1");
//        vo.setQiaGuaizbt();
                vo.setStartDate(DateUtils.format(entity.getAllowanceTime()));
                voList.add(vo);
            }
        }
        JSONObject object = new JSONObject();
        object.put("guazbtVOS",voList);
        sendMq(object, EmployeeChangeConst.SECURITY_TYPE_CREDENTIAL);
        return R.ok();
    }

    private void sendMq(Object object,String type){
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSON(object).toString());
        jsonObject.put("type",type);
        messageSender.send(employeeChangeMqConfig.SMS_EXCHANGE,employeeChangeMqConfig.SMS_ROUTING_KEY,jsonObject.toJSONString());
    }

    private BigDecimal getBigDecimal (BigDecimal a,BigDecimal b){
        if(ObjectUtils.isEmpty(a)){
            a = new BigDecimal(0);
        }
        a = ObjectUtils.isEmpty(b)?a:a.add(b);
        return a;
    }
}
